package com.crm.controller;

import com.crm.oss.client.OSSAliyunFileStorageService;
import com.crm.pojo.common.ErrorMessage;
import com.crm.pojo.common.Result;
import com.crm.service.ContractService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Author: xaw
 * @CreateTime: 2025-09-04  19:36:31
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
public class CommonController {
    private final OSSAliyunFileStorageService fileStorageService;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) throws Exception {
        try {
            // 获得原始文件名
            String originalFilename = file.getOriginalFilename();
            // 获得文件扩展名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + extension;
            String url = fileStorageService.store(fileName, file.getInputStream());
            return Result.success(url);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
